﻿Imports TransportadoraToledoBLL
Imports System.Windows.Forms

Public Class frm_cadastro_tipos_cargas

#Region "Métodos e Variáveis"
    Public objBLL As New tiposCargasBLL
    Public controle As String
    Public ultimo, primeiro, atual As Integer

    Sub carregaPrimeiroUltimo()
        Try
            ultimo = objBLL.localizarPrimeiroUltimo("ultimo")
            primeiro = objBLL.localizarPrimeiroUltimo("primeiro")
            atual = ultimo
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub


    Sub carregaUltimo()
        If (ultimo <> 0) Then
            'algum usuario foi clicado
            objBLL.localizar(ultimo)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()

            End If
        End If
    End Sub
    Sub carregaPrimeiro()
        If (primeiro <> 0) Then
            'algum usuario foi clicado
            objBLL.localizar(primeiro)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()

            End If
        End If
    End Sub

    Sub limpar()
        txtCodigo.Text = ""
        cbStatus.SelectedIndex = 0
        txtDescricao.Text = ""
        controle = ""
        objBLL.limpar()
    End Sub

    Sub status(ByVal sta As Boolean)

        If (txtCodigo.Text = "" Or controle = "A") Then
            bIncluir.Enabled = sta

            bExcluir.Enabled = sta
            If (txtCodigo.Text = "") Then
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
                bExcluir.Enabled = False
            Else
                bExcluir.Enabled = sta
                bPesquisar.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
            End If
            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta
            bPrimeiro.Enabled = sta
            bProximo.Enabled = sta
            bUltimo.Enabled = sta
            bAnterior.Enabled = sta
            gbConteudo.Enabled = Not sta
        Else
            bIncluir.Enabled = sta

            If (txtCodigo.Text = "") Then
                bExcluir.Enabled = False
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
            Else
                bExcluir.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
                bPesquisar.Enabled = sta
            End If

            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta

            gbConteudo.Enabled = Not sta
        End If

        If (atual = primeiro And atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        ElseIf (atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True
        ElseIf (atual = primeiro) Then
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        Else
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True

        End If
    End Sub
    Sub carregaPropriedades()

        Try
            If (txtCodigo.Text = "") Then
                objBLL.pCodigo = 0
            Else
                objBLL.pCodigo = txtCodigo.Text
            End If

           
            objBLL.pStatus = cbStatus.Text
            objBLL.pDescricao = txtDescricao.Text
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub carregaCampos()
        Try
            txtCodigo.Text = objBLL.pCodigo
            If (objBLL.pStatus = "A") Then
                cbStatus.SelectedIndex = 0
            Else
                cbStatus.SelectedIndex = 1
            End If


            txtDescricao.Text = objBLL.pDescricao
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub atualizaGrid()
        Try
            dgDados.DataSource = objBLL.localizar(txtConsultar.Text)
            dgDados.Columns(0).HeaderText = "Código"
            dgDados.Columns(1).HeaderText = "Nome"
            dgDados.Columns(2).HeaderText = "Status"
            dgDados.Columns(0).Width = 110
            dgDados.Columns(1).Width = 410
            dgDados.Columns(2).Width = 110


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region

    Private Sub bIncluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bIncluir.Click
        limpar()

        controle = "I"
        status(False)
    End Sub

    Private Sub bExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bExcluir.Click
        Try
            If (MessageBox.Show("Deseja excluir?", "", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
                objBLL.VerificaExclusao(Integer.Parse(txtCodigo.Text))
                If (objBLL.pVerifica = False) Then
                    carregaPropriedades()
                    objBLL.excluir()
                    MessageBox.Show("Excluido com sucesso!")
                    limpar()
                    carregaPrimeiroUltimo()
                    carregaUltimo()
                    status(True)
                Else
                    MsgBox("Tipo de Carga não pode ser excluido, pois possui vínculo com Carga!", MsgBoxStyle.Critical, "Erro")
                End If
                
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub bAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAlterar.Click
        controle = "A"

        status(False)
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        If (controle = "I") Then
            carregaPrimeiroUltimo()
            carregaUltimo()
        ElseIf (controle = "A") Then
            objBLL.localizar(atual)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()
                atual = objBLL.pCodigo
            End If
        End If
        controle = ""

        status(True)
    End Sub

    Private Sub bVaPara_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bVaPara.Click
        Try
            Dim frm As New frm_VaPara("tipos_cargas")
            frm.ShowDialog()
            If (frm.codigo <> 0) Then
                'algum usuario foi clicado
                objBLL.localizar(frm.codigo)
                If (objBLL.pCodigo <> 0) Then
                    carregaCampos()
                    atual = objBLL.pCodigo
                Else
                    Throw New Exception("Registro Inexistente!")
                End If
            Else
                Throw New Exception("Registro Inexistente!")
            End If
            status(True)
            frm = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Erro")
        End Try
    End Sub

    Private Sub bSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSalvar.Click
        Try
            carregaPropriedades()
            If (controle = "I") Then
                objBLL.inserir()
                MessageBox.Show("Incluido com sucesso!")
                limpar()
                carregaPrimeiroUltimo()
                carregaUltimo()
                status(True)
            Else
                objBLL.alterar()
                MessageBox.Show("Alterado com sucesso!")

                status(True)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub frm_cadastro_tipos_cargas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New System.Drawing.Point(Me.Location.X, 231)
        Me.Size = New Size(734, 265)
        carregaPrimeiroUltimo()
        carregaUltimo()
        bCancelar_Click(sender, e)
    End Sub

    Private Sub bSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSair.Click
        Me.Close()
    End Sub

    Private Sub bUltimo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bUltimo.Click
        carregaUltimo()
        atual = ultimo
        status(True)
    End Sub

    Private Sub bPrimeiro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPrimeiro.Click
        carregaPrimeiro()
        atual = primeiro
        status(True)
    End Sub

    Private Sub bProximo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bProximo.Click
        Dim codigo = objBLL.localizarProxAnteriro("proximo", atual)
        objBLL.localizar(codigo)
        If (objBLL.pCodigo <> 0) Then
            carregaCampos()
            atual = objBLL.pCodigo
        End If
        status(True)
    End Sub

    Private Sub bAnterior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAnterior.Click
        Dim codigo = objBLL.localizarProxAnteriro("anterior", atual)
        objBLL.localizar(codigo)
        If (objBLL.pCodigo <> 0) Then
            carregaCampos()
            atual = objBLL.pCodigo
        End If
        status(True)
    End Sub

    Private Sub bPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisar.Click

        dgDados.DataSource = Nothing
        txtConsultar.Text = ""

        If (Me.Location.Y = 231) Then
            gbConsulta.Visible = True
            Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y - 132)
            Me.Size = New Size(734, 529)
        Else
            gbConsulta.Visible = False
            Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 132)
            Me.Size = New Size(734, 265)
        End If


       
    End Sub

    Private Sub bClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bClose.Click
        gbConsulta.Visible = False
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 132)
        Me.Size = New Size(734, 265)
    End Sub

    Private Sub bFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bFiltrar.Click
        atualizaGrid()
    End Sub

    Private Sub dgDados_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgDados.CellDoubleClick
        Dim codigo As Integer
        If (e.RowIndex >= 0) Then
            codigo = dgDados.Rows(e.RowIndex).Cells("tc_codigo").Value
            objBLL.localizar(codigo)
            If (objBLL.pCodigo <> 0) Then
                carregaCampos()
                atual = objBLL.pCodigo
                gbConsulta.Visible = False
                Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 132)
                Me.Size = New Size(734, 265)
            End If
        End If
    End Sub

    Private Sub bAjuda_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAjuda.Click
        Dim Ajuda As String
        Ajuda = Application.StartupPath
        Ajuda = Ajuda.Replace("\bin\Debug", "")
        Ajuda = Ajuda & "\Arquivo de Ajuda\index.html#CadastroTipoCarga"
        Dim Ie As Object
        Ie = CreateObject("InternetExplorer.Application")

        Ie.Visible = True
        Ie.Navigate(Ajuda)
    End Sub

    Private Sub cbStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbStatus.SelectedIndexChanged
        Try

            If (cbStatus.SelectedIndex = 1) Then
                objBLL.VerificaSituacao(Integer.Parse(txtCodigo.Text))
                If (objBLL.pVerifica = False) Then

                Else
                    MsgBox("Tipo de carga não pode ser Inativado pois possui vinculo em cargas com Status Aguardando!", MsgBoxStyle.Critical, "Erro")
                    cbStatus.SelectedIndex = 0
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class